layui.define(['form', 'table'], function(exports) {
    var $ = layui.$, table = layui.table, form = layui.form;

    var placeId = $('input[name="placeId"]').val();
    var deviceTypeId = $('input[name="deviceTypeId"]').val();

    table.render({
        elem: '#LAY-table'
        ,url: layui.setter.webRoot + '/pcl/place/cost/template/list.data'
        ,where: {
            "placeId":placeId,
            "deviceTypeId":deviceTypeId
        }
        ,method: 'post'
        ,parseData: function (res) {
            return {
                "code": res.code,
                "msg": res.msg,
                "count": res.body.total,
                "data": res.body.data
            };
        }
        ,response: {statusCode: 200}
        ,cols: [[
            {field: 'id', title: '模版ID'}
            ,{field: 'title', title: '模版标题'}
            ,{field: 'deviceTypeName', title: '设备类型'}
            ,{field: 'userName', title: "所属用户",templet:function (record){
                if(record.userId == 0){
                    return '平台模版';
                }else{
                    return record.userName + "(ID:"+record.userId+")";
                }
            }}
            ,{field: 'feeInfo', title: '收费描述'}
            ,{field: 'stateName', title: '状态'}
            ,{field: 'updateDate', title: '最后修改'}
            ,{field: 'createDate', title: '创建时间'}
            ,{title: '操作', width: 80, align:'center', fixed: 'right', templet: function (record) {
                var html = '';
                if(record.currentUse == false){
                    html = html + '<a class="layui-btn layui-btn-xs" lay-event="changeTemplate">切换</a>';
                }
                return html;
            }}
        ]]
        ,page: true
        ,limit: 30
        ,height: 'full-220'
        ,text: '对不起，加载出现异常！'
    });

    //监听搜索
    form.on('submit(LAY-search)', function(data){
        var field = data.field;
        //执行重载
        table.reload('LAY-table', {
            where: field
        });
    });

    //事件
    var active = {
        changeTemplate:function (obj){
            layer.confirm('确定要切换成该收费模版吗？', function(index){
                $.ajax({
                    type: "post",
                    url: layui.setter.webRoot + '/pcl/place/cost/template/change.do',
                    data:{placeId:placeId,feeTemplateId:obj.data.id},
                    dataType:"json",
                    success:function(res){
                        if(res.code == 200){
                            layer.msg('切换成功',{icon: 1});
                            table.reload('LAY-table');
                        }else{
                            layer.msg(res.msg,{icon: 5});
                        }
                    }
                });
                layer.close(index);
            });
        }
    }

    table.on('tool(LAY-table)', function(obj){
        var type = obj.event ;
        active[type] ? active[type].call(this,obj) : '';
    });

    exports('pcl_place_cost_change_list', {})
})